/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controle;

import DAO.UsuarioDAO;
import Database.DBConnection;
import Model.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fellipe
 */
@WebServlet(name = "trataCadastroUsuario", urlPatterns = {"/trataCadastroUsuario"})
public class trataCadastroUsuario extends Comando {

    private Connection conexao;

    public void execute() throws ServletException, IOException, SQLException {
        getResponse().setContentType("text/html;charset=UTF-8");
        PrintWriter out = getResponse().getWriter();
        try {
            
            DateFormat df = DateFormat.getDateTimeInstance();
            String nome = getRequest().getParameter("nome");
            String login = getRequest().getParameter("login");
            String senha = getRequest().getParameter("senha");
            String data = df.format(new Date(System.currentTimeMillis()));
            
            Usuario user = new Usuario(nome, login, senha, data);
            new UsuarioDAO().inserir(user);
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Sistema de Publicacao de Noticias</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1> Usuario Cadastrado com Sucesso</h1>");
            out.println("Em: " + data + "<p>");
            out.println("<p><a href='menu.jsp'> Voltar </a></p>");
            //out.println("<h1>Servlet trataCadastroUsuario at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            
            getResponse().sendRedirect("usuarioCadastrado.jsp");  
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }
}
